package com.example.smartrefreshlayoutdemo.fragment.practice;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.smartrefreshlayoutdemo.R;
import com.example.smartrefreshlayoutdemo.adapter.BaseRecyclerAdapter;
import com.example.smartrefreshlayoutdemo.adapter.SmartViewHolder;
import com.scwang.smart.refresh.header.TwoLevelHeader;
import com.scwang.smart.refresh.header.listener.OnTwoLevelListener;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.scwang.smart.refresh.layout.simple.SimpleMultiListener;

import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;

public class SecondFloorPracticeFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_practice_second_floor,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        final Toolbar toolbar = view.findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });

        final View floor = view.findViewById(R.id.second_floor);
        final TwoLevelHeader header = view.findViewById(R.id.header);
        final RefreshLayout refreshLayout = view.findViewById(R.id.refreshLayout);
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                header.openTwoLevel(true);
            }
        });
        header.setOnTwoLevelListener(new OnTwoLevelListener() {
            @Override
            public boolean onTwoLevel(@NonNull RefreshLayout refreshLayout) {
                Toast.makeText(getContext(), "触发二楼事件", Toast.LENGTH_SHORT).show();
                view.findViewById(R.id.second_floor_content).animate().alpha(1).setDuration(2000);
                return true;
            }
        });

        refreshLayout.setOnMultiListener(new SimpleMultiListener(){
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(2000);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                Toast.makeText(getContext(), "触发刷新事件", Toast.LENGTH_SHORT).show();
                refreshLayout.finishRefresh(2000);
            }

            @Override
            public void onHeaderMoving(RefreshHeader header, boolean isDragging, float percent, int offset, int headerHeight, int maxDragHeight) {
                toolbar.setAlpha(1 - Math.min(percent,1));
                floor.setTranslationY(Math.min(offset - floor.getHeight() + toolbar.getHeight(),refreshLayout.getLayout().getHeight() - floor.getHeight()));
            }

            @Override
            public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
                if (oldState == RefreshState.TwoLevel) {
                    view.findViewById(R.id.second_floor_content).animate().alpha(0).setDuration(1000);
                }
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                Toast.makeText(getContext(), "触发刷新事件", Toast.LENGTH_SHORT).show();
                refreshLayout.finishRefresh(2000);
            }
        });

        final RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        /*if (recyclerView != null) {
            recyclerView.setNestedScrollingEnabled(false);
            List<Void> voids = Arrays.asList(null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
            recyclerView.setAdapter(new RecyclerViewAdapter(voids));
        }*/
    }

    public class RecyclerViewAdapter extends BaseQuickAdapter<Void,BaseViewHolder>{

        public RecyclerViewAdapter(@org.jetbrains.annotations.Nullable List<Void> data) {
            super(android.R.layout.simple_list_item_2, data);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, Void aVoid) {
            holder.setText(android.R.id.text1,getString(R.string.item_example_number_title,holder.getLayoutPosition()));
            holder.setText(android.R.id.text2,getString(R.string.item_example_number_title,holder.getLayoutPosition()));
            holder.setTextColor(android.R.id.text2,getResources().getColor(R.color.colorTextAssistant));
        }
    }
}
